﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Windows.Forms;
using Loki.Engine;
using Loki.UI;
using Loki.UI.Win;
using Luna.UI.Accounting;

namespace Luna.UI.Win.Accounting
{
    /// <summary>
    /// Accouting graphs view.
    /// </summary>
    public partial class AccountingGraphsView : FormBase, IView<AccountingGraphViewModel>
    {
        public AccountingGraphsView()
        {
            InitializeComponent();
            this.BindingChanged += AccountingCategoryListView_BindingChanged;
        }

        public new AccountingGraphViewModel ViewModel
        {
            get { return (AccountingGraphViewModel)base.ViewModel; }
            set { base.ViewModel = value; }
        }

        private void AccountingCategoryListView_BindingChanged(object P_Sender, EventArgs e)
        {
            DRP_Account.ConfigureAccounts(ViewModel.SourceAccounts);
            DRP_Account.BindValue(ViewModel, v => v.SelectedAccountID, DataSourceUpdateMode.OnPropertyChanged);

            TXT_End.BindValue(ViewModel, v => v.EndDate, DataSourceUpdateMode.OnPropertyChanged);
            TXT_Start.BindValue(ViewModel, v => v.StartDate, DataSourceUpdateMode.OnPropertyChanged);

            RG_Graphes.BindValue(ViewModel, v => v.GraphType, DataSourceUpdateMode.OnPropertyChanged, ConverterHelper.EnumString<AccountingGraphType>());

            ViewModel.SubModels.CollectionChanged += ViewModels_CollectionChanged;
            if (ViewModel.CurrentGraph != null)
            {
                CreatePages(new IViewModel[] { ViewModel.CurrentGraph });
            }
        }

        protected override void OnActivated(EventArgs e)
        {
            if (PNL_Graph.Controls.Count != 0)
            {
                ((ViewControlBase)PNL_Graph.Controls[0]).ViewModel.Activate.Execute(null);
            }

            base.OnActivated(e);
        }

        protected override void OnDeactivate(EventArgs e)
        {
            base.OnDeactivate(e);
            if (PNL_Graph.Controls.Count != 0)
            {
                ((ViewControlBase)PNL_Graph.Controls[0]).ViewModel.Desactivate.Execute(null);
            }
        }

        private void ViewModels_CollectionChanged(object P_Sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                CreatePages(e.NewItems);
            }

            if (e.OldItems != null)
            {
                RemovePages(e.OldItems);
            }
        }

        private void CreatePages(IEnumerable P_Controls)
        {
            foreach (IViewModel L_Model in P_Controls)
            {
                ViewControlBase L_Control = ContextHelper.GetView<ViewControlBase>(L_Model);

                PNL_Graph.Controls.Add(L_Control);
                L_Control.Dock = System.Windows.Forms.DockStyle.Fill;

                L_Control.ViewModel = L_Model;

                /*if (BindNameOnVMState)
                {
                    DataBinder.OneWay(L_Page, f => f.Text, L_Model, v => v.IsDirty, ConverterHelper.FormName, L_Model.DisplayName);
                }*/

                L_Model.LoadData();
            }
        }

        private void RemovePages(IEnumerable P_Controls)
        {
            foreach (IViewModel L_Model in P_Controls)
            {
                ViewControlBase L_Page = PNL_Graph.Controls.OfType<ViewControlBase>().FirstOrDefault(x => x.ViewModel == L_Model);
                if (L_Page != null)
                {
                    PNL_Graph.Controls.Remove(L_Page);
                    L_Model.Dispose();
                    L_Page.Dispose();
                }
            }
        }
    }
}
